package com.sound 
{
	import components.Singleton;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.utils.Dictionary;

	/**
	 * ...
	 * @author wen
	 */
	public class SoundCtrl extends Singleton
	{
		private  var _soundOn:Boolean = true;
		
		private var _soundPlay:SoundChannel = null;
		
		private var _soundRec:Dictionary = null;
		
		private var _isInit:Boolean = false;
		
		private var _musicOn:Boolean = true;
		
		private var _soundEffectOn:Boolean = true;
		
		
		public function SoundCtrl() 
		{
			init();
		}
		
		public function init():void
		{
			if (_isInit) return;
			_isInit = true;
			
			_soundPlay = new SoundChannel();
			
			_soundRec = new Dictionary();
			
			_soundRec['bg'] 	= new zloop();
			_soundRec['bell'] 	= new ztink();
			_soundRec['bird'] 	= new zbonus();
		}
		
		
		public function soundPlay(type:String):void
		{
			switch(type)
			{
				case "bg":
					if (_musicOn)
					{
						_soundPlay = (_soundRec['bg'] as Sound).play(0,int.MAX_VALUE);
					}
					break;
				case "bell":
					if (_soundEffectOn)
					{
						_soundPlay = (_soundRec['bell'] as Sound).play();
					}
					break;
				case "bird":
					if (_soundEffectOn)
					{
						_soundPlay = (_soundRec['bird'] as Sound).play();
					}
					break;
			}
		}
		
		public function clear():void
		{
			_soundPlay.stop();
		}
		
		public static function get instance():SoundCtrl
		{
			return Singleton.getInstance(SoundCtrl) as SoundCtrl;
		}
		
		public function get musicOn():Boolean 
		{
			return _musicOn;
		}
		
		public function set musicOn(value:Boolean):void 
		{
			_musicOn = value;
			
			if (!_musicOn)
			{
				clear();
			}
		}
		
		public function get soundEffectOn():Boolean 
		{
			return _soundEffectOn;
		}
		
		public function set soundEffectOn(value:Boolean):void 
		{
			_soundEffectOn = value;
		}
	}
}